//
//  EditNickNameController.m
//  History
//
//  Created by 七 on 16/9/10.
//  Copyright © 2016年 kane. All rights reserved.
//

#import "EditNickNameController.h"

@interface EditNickNameController ()
{
    IBOutlet UIView *BGView;
    __weak IBOutlet UIView *navView;
    __weak IBOutlet UILabel *lineLbl;
    __weak IBOutlet UITextField *nickTF;
    __weak IBOutlet UIButton *saveBtn;
    __weak IBOutlet UILabel *titleLbl;
}
@end

@implementation EditNickNameController

- (void)viewDidLoad {
    [super viewDidLoad];
    BGView.backgroundColor = KKColor_BGColor_GrayToDarkgray;
    lineLbl.backgroundColor = KKColor_NavLineColor;
    navView.backgroundColor = KKColor_DarkGray_White;
    titleLbl.textColor = KKColor_Black_LightGary;
    saveBtn.layer.cornerRadius = 5.0;
    nickTF.text = [[UserEntity sharedUser].NickName convertToString];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - User Action
- (IBAction)saveBtnPress:(id)sender {
    if (nickTF.text.length < 4 || nickTF.text.length>40) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"昵称保存失败" message:@"用户昵称过长或过短" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }
    NSDictionary *params = @{@"c_id":[UserEntity sharedUser].UserID,@"c_nick":nickTF.text};
    [WebService modifyNickName:params WithBlock:^(id result, NSError *error) {
        if (!error) {
            if ([result[@"errcode"] integerValue] == 0) {
                NSDictionary *customer = result[@"result"][@"customer"][@"value"];
                [UserEntity sharedUser].NickName = [customer[@"c_nick"] convertToString];
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"修改成功" delegate:nil cancelButtonTitle:@"好" otherButtonTitles:nil, nil];
                [alert show];
                [self.navigationController popViewControllerAnimated:YES];
            }else{
                [self.view makeToast:[result[@"errmsg"] convertToString] position:@"center"];
            }
        }
    }];
    
}
- (IBAction)backBtnPress:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

//隐藏键盘
- (void)hideKeyboard
{
    [nickTF resignFirstResponder];
}


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self hideKeyboard];
}
@end
